{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "13fa1087",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Defaulting to user installation because normal site-packages is not writeable\n",
      "Requirement already satisfied: openai in /Users/mbj0593/Library/Python/3.9/lib/python/site-packages (0.27.6)\n",
      "Requirement already satisfied: requests>=2.20 in /Users/mbj0593/Library/Python/3.9/lib/python/site-packages (from openai) (2.30.0)\n",
      "Requirement already satisfied: tqdm in /Users/mbj0593/Library/Python/3.9/lib/python/site-packages (from openai) (4.65.0)\n",
      "Requirement already satisfied: aiohttp in /Users/mbj0593/Library/Python/3.9/lib/python/site-packages (from openai) (3.8.4)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /Users/mbj0593/Library/Python/3.9/lib/python/site-packages (from requests>=2.20->openai) (3.1.0)\n",
      "Requirement already satisfied: idna<4,>=2.5 in /Users/mbj0593/Library/Python/3.9/lib/python/site-packages (from requests>=2.20->openai) (3.4)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/mbj0593/Library/Python/3.9/lib/python/site-packages (from requests>=2.20->openai) (1.26.6)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /Users/mbj0593/Library/Python/3.9/lib/python/site-packages (from requests>=2.20->openai) (2023.5.7)\n",
      "Requirement already satisfied: attrs>=17.3.0 in /Users/mbj0593/Library/Python/3.9/lib/python/site-packages (from aiohttp->openai) (23.1.0)\n",
      "Requirement already satisfied: multidict<7.0,>=4.5 in /Users/mbj0593/Library/Python/3.9/lib/python/site-packages (from aiohttp->openai) (6.0.4)\n",
      "Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /Users/mbj0593/Library/Python/3.9/lib/python/site-packages (from aiohttp->openai) (4.0.2)\n",
      "Requirement already satisfied: yarl<2.0,>=1.0 in /Users/mbj0593/Library/Python/3.9/lib/python/site-packages (from aiohttp->openai) (1.9.2)\n",
      "Requirement already satisfied: frozenlist>=1.1.1 in /Users/mbj0593/Library/Python/3.9/lib/python/site-packages (from aiohttp->openai) (1.3.3)\n",
      "Requirement already satisfied: aiosignal>=1.1.2 in /Users/mbj0593/Library/Python/3.9/lib/python/site-packages (from aiohttp->openai) (1.3.1)\n",
      "\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.1.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.2.1\u001b[0m\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49m/Library/Developer/CommandLineTools/usr/bin/python3 -m pip install --upgrade pip\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "!pip install openai "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2c034927",
   "metadata": {},
   "outputs": [],
   "source": [
    "#import os\n",
    "#os.environ[\"OPENAI_API_TYPE\"]='azure'\n",
    "#os.environ[\"OPENAI_API_VERSION\"]='2023-08-01-preview'\n",
    "#os.environ[\"OPENAI_API_BASE\"]='https://<Your EndPoint>/'\n",
    "#os.environ[\"OPENAI_API_KEY\"]='<Your Key>''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d08aa43a",
   "metadata": {},
   "outputs": [],
   "source": [
    "deployment = \"gpt4\"\n",
    "model = \"gpt-4\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31f410fa",
   "metadata": {},
   "source": [
    "# Hello GPT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5dd7f3da",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好！我是OpenAI的虚拟助理，我没有具体的名字，你可以随便叫我。我在这里帮助你解答任何问题和完成各种任务。\n"
     ]
    }
   ],
   "source": [
    "import openai\n",
    "\n",
    "response = openai.ChatCompletion.create(\n",
    "    engine=deployment, # 如果直接访问OpenAI GPT服务的同学，这里不要使用engine这个参数，要使用model，如： model=“gpt-4”\n",
    "    model = model, # for OpenAI \n",
    "    messages=[\n",
    "        {\"role\": \"system\", \"content\": \"你是一个AI助理\"},   \n",
    "        {\"role\": \"user\", \"content\": \"你好！你叫什么名字？\"}\n",
    "    ],\n",
    "    temperature = 0.9, \n",
    "    max_tokens = 2000,  \n",
    "  )\n",
    "print(response.choices[0].message.content)\n",
    "#print(response)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4c95c2c8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['你好！我是OpenAI的虚拟助理，我没有具体的名字，你可以随便叫我。我在这里帮助你解答任何问题和完成各种任务。', '你好！我是OpenAI的GPT-3模型，一种未命名的人工智能助手。我可以帮助回答你的问题、获取信息、创建内容等。', '你好！我是OpenAI的人工智能助理，我没有正式的名字，你可以叫我AI助手。']\n"
     ]
    }
   ],
   "source": [
    "print([choice.message.content for choice in response.choices])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d9ff976",
   "metadata": {},
   "source": [
    "# Token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "3b04096b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: tiktoken in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (0.4.0)\r\n",
      "Requirement already satisfied: regex>=2022.1.18 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from tiktoken) (2022.7.9)\r\n",
      "Requirement already satisfied: requests>=2.26.0 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from tiktoken) (2.31.0)\r\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from requests>=2.26.0->tiktoken) (2.0.4)\r\n",
      "Requirement already satisfied: idna<4,>=2.5 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from requests>=2.26.0->tiktoken) (3.4)\r\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from requests>=2.26.0->tiktoken) (1.26.16)\r\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from requests>=2.26.0->tiktoken) (2023.7.22)\r\n"
     ]
    }
   ],
   "source": [
    "!pip install tiktoken"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "54b967e9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[19000, 39442, 37507, 98806, 81543, 28037, 37507, 9554, 13646, 20022, 247, 3922, 60843, 31634, 19361, 17792, 24326, 232, 8676, 225, 6701, 249, 67178, 20834, 37507, 3922, 45932, 96, 19483, 17792, 51611, 76982, 21043, 98739, 1811]\n",
      "chinese:在未来还没有到来的时候，总要有人把它创造出来，那个人应该是我们。 ; 35 tokens\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import openai\n",
    "import tiktoken\n",
    "\n",
    "encoding = tiktoken.encoding_for_model(\"gpt-4\")\n",
    "\n",
    "chinese = \"\"\"在未来还没有到来的时候，总要有人把它创造出来，那个人应该是我们。\"\"\"\n",
    "\n",
    "tokens = encoding.encode(chinese)\n",
    "\n",
    "print(tokens)\n",
    "\n",
    "num_of_tokens_in_chinese = len(encoding.encode(chinese))\n",
    "\n",
    "print(f\"chinese:{chinese} ; {num_of_tokens_in_chinese} tokens\\n\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea48fe92",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
